package com.future;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 228. 汇总区间
 *
 * @author weiruibai.vendor
 * Date: 2023/3/24 09:22
 */
public class Solution_228 {

    private static Solution_228 getInstance() {
        return new Solution_228();
    }

    public static void main(String[] args) {
        int[] nums = new int[]{0, 1, 2, 4, 5, 7};
        System.out.println(getInstance().summaryRanges(nums));
    }

    public List<String> summaryRanges(int[] nums) {
        List<String> ans = new ArrayList<>();
        if (nums == null || nums.length == 0) {
            return ans;
        }
        int N = nums.length;
        if (N == 1) {
            ans.add(nums[0] + "");
            return ans;
        }
        for (int i = 0; i < N; ) {
            int start = nums[i];
            int end = nums[i];
            int j = i + 1;
            while (j < N && nums[j - 1] + 1 == nums[j]) {
                end = nums[j];
                j++;
            }
            String str = start + "";
            if (start != end) {
                str += "->" + end;
            }
            ans.add(str);
            i = i == j ? i++ : j;
        }
        return ans;
    }
}
